from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.remote.webdriver import By
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import pymysql

def insert_journal_info(name):
    # 建立数据库连接
    conn = pymysql.connect(
        host='localhost',
        port=3306,
        user='root',
        password='zhaolonghui123',
        db='fastapi',
    )

    # 获取游标对象
    cursor = conn.cursor()

    chrome_options = Options()
    chrome_options.add_argument('--disable-blink-features=AutomationControlled') # 禁用Selenium检测
    driver = webdriver.Chrome(options=chrome_options)
    driver.get('https://w.wanfangdata.com.cn/')
    time.sleep(1)
    WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.XPATH,"/html/body/div[6]/div[1]/div/div[1]/div[2]/div/div[1]/span[1]"))
    ).click()
    WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.XPATH,"""//*[@id="search-picker-popover"]/ul/li[2]/span"""))
    ).click()
    WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.XPATH,"""//*[@id="search-input"]"""))
    ).send_keys(f"刊名:{name}")
    WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.XPATH,"""//*[@id="search-btn-document"]/span"""))
    ).click()
    while True:
        for i in range(1,21):
            item = WebDriverWait(driver, 15).until(
                EC.presence_of_element_located((By.XPATH,f"""/html/body/div[5]/div/div[2]/div[2]/div/div[2]/div[2]/div[3]/div[{str(i)}]"""))
            )

            papername = item.find_element(By.XPATH,"./div[1]").text
            last_dot_index = papername.find('.')
            if last_dot_index != -1:
                papername = papername[last_dot_index + 1:]

            authorlist = item.find_elements(By.XPATH, "./div[2]/span")
            list = []
            for author in authorlist:
                if len(author.text) > 0 and (author.text[0] not in ["[", "-", "《", "2"]):
                    list.append(author.text)
                if len(author.text) > 0 and author.text[0] == "2":
                    publish = author.text
            if list == []:
                authorlist = "no author"
            else:
                authorlist = ",".join(list)

            try:
                webdownload = item.find_element(By.XPATH,"./div[5]")
            except NoSuchElementException:
                webdownload = item.find_element(By.XPATH,"./div[4]")

            last_dot_index = webdownload.text.find('下载：')
            if last_dot_index != -1:
                webdownload = webdownload.text[last_dot_index + 3:]
            if not isinstance(webdownload, str):
                webdownload = "0"

            data = [
                (papername, authorlist, publish, name, int(webdownload)),
            ]
            cursor.executemany("""
                INSERT INTO Journal_information (papername, author, publish, journalname, webdownload) VALUES (%s, %s, %s, %s, %s)
            """, data)
            conn.commit()

        WebDriverWait(driver, 10).until(
            EC.presence_of_element_located(
                (By.XPATH, """/html/body/div[5]/div/div[2]/div[2]/div/div[2]/div[2]/div[1]/div[5]/div[4]/span[3]"""))
        ).click()

        # 判断页面是否已经翻到最后一页，如果是则退出循环
        try:
            WebDriverWait(driver, 5).until(
                EC.presence_of_element_located(
                    (By.XPATH, """/html/body/div[5]/div/div[2]/div[2]/div/div[2]/div[2]/div[3]/div[1]"""))
            )
        except:
            break

        time.sleep(5)

    cursor.close()
    conn.close()